<?php
header("Content-type:text/html;charset=utf-8");

include("./inc/head.php");

//echo 'fdsa'.$_COOKIE['ECSCP']['admin_id'],$_SESSION['admin_id'];
$eAID = $_REQUEST['AID'];
//include('./inc/ZoshowFunc.php');
//$zoshow_admins = getAdminList();
$UID = $currAdmin->AID;
$Admin = $currAdmin;

if(($currAdmin->Power & 2047) == 2047)
{
	$UID = 1;
	$Admin = new Admin(array("AID"=>$UID));
}

$admins = $Admin->allchildren();
$list = "";

$isChild == false;
for($i = 0; $i < count($admins); $i++)
{
	if($eAID == $admins[$i]->AID)
		$isChild =true;
	$attr = "|-";
	for($k=0;$k<$admins[$i]->grade;$k++)
	{
		$attr = "　".$attr;
	}
//	if($zoshow_admins[$admins[$i]->AName])
//	{
//		$admins[$i]->AName = $zoshow_admins[$admins[$i]->AName]['user_name'];
//	}
	if($Admin->AID == $admins[$i]->AID)
		$list .= "<tr><td>$attr".$admins[$i]->AName."</td></tr>";
	else
		$list .= "<tr><td>$attr<a href='?AID=".$admins[$i]->AID."'>".$admins[$i]->AName."</a></td></tr>";
}
if(!$isChild && is_numeric($eAID))
{
	alert('');
}
if(is_numeric($eAID))
	$Admin->getObjById($eAID, "AID", "Admin");

if($_POST['doPost'] == "true")
{

	$Admin = new Admin($_POST);
	//var_dump($Admin);
	if(strlen($Admin->APwd) > 3 && $Admin->APwd == $Admin->ConfirmPwd)
	{
		$Admin->APwd = md5($Admin->APwd);
		$sqlField = ",APwd";
	}

	$Admin->Power = 0;
	foreach($_POST['Power'] as $power){
		$Admin->Power |= (int)$power;
	}
	
	foreach($_POST['CPower'] as $cpower){
		unset($CPower);
		list($CPower['MID'],$CPower['MUrl'],$CPower['MName']) = split(",",$cpower);
		$MCPower[] = $CPower;
	}
	$Admin->CPower = serialize($MCPower);
	
	foreach($_POST['TPower'] as $tpower)
	{
		unset($TPower);
		list($TPower['TID'],$TPower['MUrl'],$TPower['TName']) = split(",",$tpower);
		$MTPower[] = $TPower;
	}
	$Admin->TPower = serialize($MTPower);
	
	//note 一级菜单权限
	$Admin->MPower = implode(',', $_POST['menu'] );
	
	if(is_numeric($eAID) && $eAID > 0)
	{
		$req = $Admin->update("Admin", "AName,PenName,Power,CPower,TPower$sqlField,MPower", "AID");
	}
	else
	{
		$Admin->AddDate = time();
		$Admin->PID = $currAdmin->AID;
		$req = $Admin->add("Admin", "AName,PenName,PID,Power,CPower,AddDate,TPower$sqlField,MPower", "AID");
	}
	if($req)
	{
		new ActionLog(array("LogType"=>"admin_success","Action"=>"{$eAID},{$Admin->SID}"));
		alert("sucess","location","admin.php?AID=$eAID");
	}
	else
	{
		new ActionLog(array("LogType"=>"admin_err","Action"=>"{$eAID},{$Admin->SID}"));
		alert("err","location","admin.php?AID=$eAID");
	}
	exit;
}

//note 一级菜单
$sql = "SELECT * FROM menu WHERE pid = 0 ORDER BY sort ASC";
$menu = $db->getAll( $sql );
?>
<form method="post">
<input type="hidden" value="true" name="doPost">
<input type="hidden" value="<?php echo $eAID; ?>" name="AID">

<table width="90%" cellpadding="1" cellspacing="1" class="list-div">
	<tr>
		<td width="100">项目</td>
		<td colspan="3">值</td>
    </tr>
	<tr>
		<td>用户名</td>
		<td colspan="3">
		<?php if($_REQUEST['type'] == 'username'){?>
		<input type="text" name="AName" value="<?php echo $Admin->AName; ?>">
		<?php }elseif($eAID > 0){ ?>
		<input type="text" name="AName" value="<?php echo $Admin->AName; ?>" readonly="readonly">
		<?php } ?>
		</td>
    </tr>
	<tr>
		<td>别名</td>
		<td colspan="3">
		<input type="text" name="PenName" value="<?php echo $Admin->PenName; ?>">
		</td>
    </tr>
	<tr>
		<td>密码</td>
		<td colspan="3"><input type="password" name="APwd" value=""></td>
    </tr>
	<tr>
		<td>重复密码</td>
		<td colspan="3"><input type="password" name="ConfirmPwd" value=""></td>
    </tr>
     <tr>
		<td>邮件地址</td>
		<td colspan="3"><input type="text" name="AEmail" value="<?php echo $Admin->AEmail; ?>" style=" width:300px;" ></td>
    </tr>
    <tr>
		<td>导航菜单</td>
	  <td colspan="3"><?php 
		$menu_arr = $Admin->MPower ? explode(',',$Admin->MPower) : array();
		foreach( $menu as $v ){?>
      <label style="margin-right:10px;"><input type="checkbox" name="menu[]" value="<?php echo $v['mid'];?>" <?php if( in_array( $v['mid'], $menu_arr)){?>checked="checked"<?php }?> onclick="Menu(<?php echo $v['mid'];?>, this)"/><?php echo $v['name'];?></label><?php }?><span style="color:#F00" id="menu_tip"></span></td>
    </tr>
<!--	<tr>
	  <td>菜单权限</td>
	  <td colspan="3"><label><input type="checkbox"/>浏览</label>
      <label><input type="checkbox" />添加</label> 
      <label><input type="checkbox"/>删除</label>
      <label><input type="checkbox" />修改</label>
      <label><input type="checkbox" />排序</label></td>
    </tr>-->
	<tr>
		<td width="100">权限</td>
		<td colspan="3">
		<?php
		$typeKey = array("浏览",'添加','删除','修改','模板','模板域','修改产品价格','','','','');
		$adminP = 0;
        
		for($i = 0; $i < count($typeKey); $i++)
		{
			
			if(($currAdmin->Power &  pow(2,$i)) == pow(2,$i) && $typeKey[$i] != '')
			{
			echo "<input type=\"checkbox\" name=\"Power[]\" value=\"".(pow(2,$i))."\"";
			if(($Admin->Power &  (pow(2,$i))) == pow(2,$i))
				echo " checked=\"checked\"";
			echo ">{$typeKey[$i]}\n";
			}
			$adminP += pow(2,$i);
		}
			if(($currAdmin->Power &  $adminP) == $adminP)
			{
			echo "<input type=\"checkbox\" name=\"Power[]\" value=\"".$adminP."\"";
			if(($Admin->Power &  $adminP) == $adminP)
				echo " checked=\"checked\"";
			echo ">管理员\n";
			}
		?>
		
		</td>
	</tr>
	<tr>
		<td width="100" valign="top">模块权限</td>
		<td width="400" valign="top">
		<?php
		if(($currAdmin->Power & 2047) == 2047)
		{
			$m = new Mould(array("MID"=>0));
			$moulds = $m->allchildren();
			for($i = 0;$i <count($moulds); $i++)
			{
				if($moulds[$i]->PID == 0)
				{
					$attr = "|-";
					$showInList = true;
				}
				else
				{
					if($moulds[$i]->PID == $moulds[$i-1]->MID)
						$attr = "　".$attr;
				}
				$Ms[$i]['MID'] = $moulds[$i]->MID;
				$Ms[$i]['MUrl'] = $moulds[$i]->MUrl;
				$Ms[$i]['MName'] = $attr . $moulds[$i]->MName;
			}
		}
		else
			$Ms = unserialize($currAdmin->CPower);
		for($i = 0; $i < count($Ms); $i++)
		{
			echo "<input t=\"t_{$Ms[$i]['MID']}\" type=\"checkbox\" name=\"CPower[]\" value=\"{$Ms[$i]['MID']},{$Ms[$i]['MUrl']},{$Ms[$i]['MName']}\" id=\"CPower_$i\"";
			if(strInArray($Ms[$i]['MID'], unserialize($Admin->CPower)))
				echo " checked=\"checked\"";
			echo ">{$Ms[$i][MName]}<br />\n";
		}
		?>
		<input type="checkbox" onclick="for(i=0;i<<?php echo $i; ?>;i++){if(this.checked)document.getElementById('CPower_'+i).checked=true;else document.getElementById('CPower_'+i).checked=false;}">全部选定
		</td>
        
        		<td width="100" valign="top">模板权限</td>
		<td valign="top">
		<?php 
		if(($currAdmin->Power & 2047) == 2047)
		{
			$sql = "select * from Template order by TID desc";
			$result = $db->query($sql);
			$i = 0;
			while($row = $db->fetch_array($result))
			{
				if(strInArray($row[TID], unserialize($currAdmin->TPower)) 
				|| (($currAdmin->Power & 2047) == 2047 && $currAdmin->parent()==NULL 
				|| $row[UID] == $currAdmin->AID
				)){
					echo "<input type=\"checkbox\" name=\"TPower[]\" value=\"{$row[TID]},bin/docList_{$row[TID]}.php,{$row[TName]}\" id=\"TPower_$i\"";
					if(strInArray($row[TID], unserialize($Admin->TPower)))
						echo " checked=\"checked\"";
					echo ">$row[TName]<br />\n";
					$i++;
				}
			}
		}else{
			$row = unserialize($currAdmin->TPower);
			if(!empty($row)){
				for($i = 0; $i < count($row); $i++)
				{
					echo "<input type=\"checkbox\" name=\"TPower[]\" value=\"{$row[TID]},bin/docList_{$row[TID]}.php,{$row[TName]}\" id=\"TPower_$i\"";
					if(strInArray($row[$i][TID], unserialize($Admin->TPower)))
						echo " checked=\"checked\"";
					echo ">{$row[$i][TName]}<br />\n";
				}
			}
		}
		?>
		<input type="checkbox" onclick="for(i=0;i<<?php echo $i; ?>;i++){if(this.checked)document.getElementById('TPower_'+i).checked=true;else document.getElementById('TPower_'+i).checked=false;}">全部选定
	  </td>
        
	</tr>
	<!--tr>
		<td width="100">说明</td>
		<td><textarea name="AInfo" cols="80" rows="20"><?php echo htmlspecialchars($Admin->AInfo); ?></textarea></td>
	</tr-->
	<tr>
		<td width="100"></td>
		<td><input type="submit" name="sub" value="提交"></td>
        <td></td>
        <td></td>
	</tr>
</table>

<!--列表开始-->
<table><?php
echo $list;
?></table>
<!--列表结束-->

</form>

<script type="text/javascript">
//note 选择导航菜单,未选中菜单时，所有属于其下的所有模块也不选择
function Menu( mid, obj ){
	$("#menu_tip").html("正在读取数据，请稍候操作...");
	$.get('./bg/ajax.php?m=menu_no_select&mid='+mid,function(data){//alert(data);
		if(data){
			var arr = data.split(',');
			for( var i in arr ){
				if( !$(obj).attr("checked") ){
					$("input[t='t_"+arr[i]+"']").attr("checked","");
					$("input[t='t_"+arr[i]+"']").attr("disabled","disabled");
				}else{
					$("input[t='t_"+arr[i]+"']").attr("disabled","");
				}
			}
		}
		$("#menu_tip").html("");
	});
}
</script>
<?php
include("inc/foot.php");
?>
